Skip to content

Conversation

RanVaknin
Copy link
Contributor

@RanVaknin RanVaknin commented Sep 5, 2025

This PR adds:

  1. New utils-lite code package that provides SdkInternalThreadLocal, a wrapper around threadLocal.
  2. Support for trace id propagation using the new utils-lite class

Background

Previously, we implemented trace ID propagation using SLF4J's MDC in PR #6363, but this was
reverted because the MDC interface exists but the implementation is not provided by the SDK, Lambda runtime, or X-Ray SDK.

Solution

Added a small utils-lite utility class that provides thread local key value storage using ThreadLocal<Map<String, String>>. For this case, it allows the Lambda Runtime Interface Client, AWS SDK, and X-Ray SDK to share trace context via this one package, but can extended to other use cases.

Example:

SdkInternalThreadLocal.put("some-value", foo);
String traceId = SdkInternalThreadLocal.get("some-value");
SdkInternalThreadLocal.remove("some-value");
SdkInternalThreadLocal.clear();

@RanVaknin RanVaknin requested a review from a team as a code owner September 5, 2025 17:59
@RanVaknin RanVaknin added the api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team label Sep 5, 2025
@RanVaknin RanVaknin force-pushed the feature/master/utils-lite-lambda-trace branch from 9badcf1 to 85642fe Compare September 11, 2025 17:58
@RanVaknin RanVaknin force-pushed the feature/master/utils-lite-lambda-trace branch from 18ce92a to 8462a02 Compare September 12, 2025 20:01
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
74.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

return context.httpRequest();
}

@Override
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) {
saveTraceId(executionAttributes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not super obvious why we invoke saveTraceId in afterExecution and onExecutionFailure, can we add some javadocs?


@Override
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
String traceId = SdkInternalThreadLocal.get(CONCURRENT_TRACE_ID_KEY);
Copy link
Contributor

@zoewangg zoewangg Sep 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check lambdaFunctionNameEnvironmentVariable() and only check trace ID if it is a lambda function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants